﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using University.Workshop.Business;
using University.Workshop.Presenters;
using University.Workshop.Model;

namespace University.Workshop.WinApp
{
    public partial class CheckupRegister : Form, IShowConsultation
    {
        string FamilyHistory = String.Empty;
        string PastMedicalHistory = String.Empty;
        string PersonalHistory = String.Empty;
        string GeneralSymptoms = String.Empty;

        public string PatientId;
        public string DoctorId;
        public string AppointmentId;
        public string ConsultationId;


        public CheckupRegister()
        {
            InitializeComponent();
            cbxCurrentCondition.SelectedIndex = 0;
        }

        private void tabPersonalHistory_Click(object sender, EventArgs e)
        {

        }

        private void tabSignosPaciente_Click(object sender, EventArgs e)
        {

        }

        private void splitContainer1_Panel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void splitContainer1_Panel2_Paint(object sender, PaintEventArgs e)
        {

        }

        private void btnCancelar_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void btnGuardar_Click(object sender, EventArgs e)
        {
            //ConsultationManager cm = new ConsultationManager();
            try
            {
                VerifyFamilyHistoryCheckBoxes();

                VerifyPastMedicalHistoryCheckBoxes();

                VerifyPersonalHistoryCheckBoxes();

                VerifyGeneralSymptomsCheckBoxes();

                ConsultationManager.RegisterNewConsultation(

                //cm.RegisterNewConsultation(
                    txtPatientId.Text,
                    txtDoctorId.Text,
                    int.Parse(txtAppointmentId.Text),


                    FamilyHistory,
                    txtFamilyHistoryDetails.Text,
                    PastMedicalHistory,
                    txtPastMedicalHistoryDetails.Text,
                    PersonalHistory,
                    txtPersonalHistoryDetails.Text,

                    txtHeartRate.Text,
                    txtBloodPressure.Text,
                    txtRespirationRate.Text,
                    txtTemperature.Text,
                    txtCurrentWeight.Text,
                    txtHeight.Text,
                    txtIdealWeight.Text,

                    txtChiefComplaints.Text,
                    GeneralSymptoms,

                    cbxCurrentCondition.Text,
                    txtCurrentConditionDetails.Text,
                    txtPreviousDiagnoses.Text,
                    txtPreviouslyEmployedTherapeutic.Text,

                    txtDigestiveApparatus.Text,
                    txtCardiovascularApparatus.Text,
                    txtRespiratoryApparatus.Text,
                    txtUrinaryApparatus.Text,
                    txtGenitalApparatus.Text,
                    txtHematologicApparatus.Text,
                    txtEndocrineSystem.Text,
                    txtOsteomuscularSystem.Text,
                    txtNervousSystem.Text,
                    txtSensorialSystem.Text,
                    txtPsicosomatic.Text,

                    txtGeneralExamination.Text,
                    txtExaminationHead.Text,
                    txtExaminationNeck.Text,
                    txtExaminationChest.Text,
                    txtExaminationAbdomen.Text,
                    txtExaminationMembers.Text,
                    txtExaminationGenitals.Text,

                    
                    txtComments.Text,
                    txtDiagnosis.Text,
                    txtPrognosis.Text,
                    txtTreatment.Text
                );
                MessageBox.Show("Consulta registrada exitosamente");
                this.Dispose();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error " + ex);
                
            }
        }

        private void VerifyGeneralSymptomsCheckBoxes()
        {
            if (chkAsthenia.Checked)
                GeneralSymptoms += "1-";
            if (chkAdynamia.Checked)
                GeneralSymptoms += "2-";
            if (chkAnorexia.Checked)
                GeneralSymptoms += "3-";
            if (chkFever.Checked)
                GeneralSymptoms += "4-";
            if (chkWeightLoss.Checked)
                GeneralSymptoms += "5";
        }

        private void VerifyPersonalHistoryCheckBoxes()
        {
            if (chkSmoking.Checked)
                PersonalHistory += "1-" + txtSmoking.Text + "-";
            if (chkAlcoholism.Checked)
                PersonalHistory += "2-" + txtAlcoholism.Text + "-";
            if (chkAddiction.Checked)
                PersonalHistory += "3-" + txtAddiction.Text + "-";
            if (chkPhysicalActivity.Checked)
                PersonalHistory += "4-" + txtPhysicalActivity.Text + "-";
            if (chkImmunizations.Checked)
                PersonalHistory += "5-";
            if (chkAllergies.Checked)
                PersonalHistory += "6-";
        }

        private void VerifyPastMedicalHistoryCheckBoxes()
        {
            if (chkChildhoodInfectious.Checked)
                PastMedicalHistory += "1-";
            if (chkTBPato.Checked)
                PastMedicalHistory += "2-";
            if (chkVenerealDiseases.Checked)
                PastMedicalHistory += "3-";
            if (chkTyphoidFever.Checked)
                PastMedicalHistory += "4-";
            if (chkSalmonellosis.Checked)
                PastMedicalHistory += "5-";
            if (chkPneumonia.Checked)
                PastMedicalHistory += "6-";
            if (chkMalaria.Checked)
                PastMedicalHistory += "7-";
            if (chkParasites.Checked)
                PastMedicalHistory += "8-";
            if (chkAllergicDiseases.Checked)
                PastMedicalHistory += "9-";
            if (chkArticularConditions.Checked)
                PastMedicalHistory += "10-";
            if (chkSurgeries.Checked)
                PastMedicalHistory += "11-";
            if (chkHospitalizations.Checked)
                PastMedicalHistory += "12-";
            if (chkInjuries.Checked)
                PastMedicalHistory += "13-";
            if (chkLossConsciousness.Checked)
                PastMedicalHistory += "14-";
            if (chkDrugIntolerance.Checked)
                PastMedicalHistory += "15-";
            if (chkTransfusions.Checked)
                PastMedicalHistory += "16";
        }

        private void VerifyFamilyHistoryCheckBoxes()
        {
            if (chkTB.Checked)
                FamilyHistory += "1-";
            if (chkDiabetes.Checked)
                FamilyHistory += "2-";
            if (chkMellitus.Checked)
                FamilyHistory += "3-";
            if (chkHypertension.Checked)
                FamilyHistory += "4-";
            if (chkCarcinomas.Checked)
                FamilyHistory += "5-";
            if (chkHeart.Checked)
                FamilyHistory += "6-";
            if (chkLiver.Checked)
                FamilyHistory += "7-";
            if (chkKidney.Checked)
                FamilyHistory += "8-";
            if (chkEndocrine.Checked)
                FamilyHistory += "9-";
            if (chkMentalIllness.Checked)
                FamilyHistory += "10-";
            if (chkEpilepsy.Checked)
                FamilyHistory += "11-";
            if (chkAsthma.Checked)
                FamilyHistory += "12-";
            if (chkHematologicDiseases.Checked)
                FamilyHistory += "13";
        }

        private void btnRegistrarReceta_Click(object sender, EventArgs e)
        {
            AddPrescriptionView AddPrescriptionView;
            AddPrescriptionView = new AddPrescriptionView();
            AddPrescriptionView.Visible = true;
        }

        private void chkSmoking_CheckedChanged(object sender, EventArgs e)
        {
            if (chkSmoking.Checked)
                txtSmoking.Enabled = true;
            else
                txtSmoking.Enabled = false;
        }

        private void chkAlcoholism_CheckedChanged(object sender, EventArgs e)
        {
            if (chkAlcoholism.Checked)
                txtAlcoholism.Enabled = true;
            else
                txtAlcoholism.Enabled = false;
        }

        private void chkAddiction_CheckedChanged(object sender, EventArgs e)
        {
            if (chkAddiction.Checked)
                txtAddiction.Enabled = true;
            else
                txtAddiction.Enabled = false;
        }

        private void chkPhysicalActivity_CheckedChanged(object sender, EventArgs e)
        {
            if (chkPhysicalActivity.Checked)
                txtPhysicalActivity.Enabled = true;
            else
                txtPhysicalActivity.Enabled = false;
        }

        private void btnBuscarReserva_Click(object sender, EventArgs e)
        {
            SearchAppointmentCheckup searchAppointmentForm;
            searchAppointmentForm = new SearchAppointmentCheckup(this);
            //searchAppointmentForm.Visible = true;
            searchAppointmentForm.ShowDialog();

            txtPatientId.Text = PatientId;
            txtDoctorId.Text = DoctorId;
            txtAppointmentId.Text = AppointmentId;
            txtConsultationId.Text = ConsultationId;


            if (string.IsNullOrEmpty(PatientId) ||
                string.IsNullOrEmpty(DoctorId) ||
                string.IsNullOrEmpty(AppointmentId))
            {
            }
            else
            {
                btnGuardar.Enabled = true;
                btnGuardar.Visible = true;

                //btnRegistrarInsumos.Enabled = true;
                //btnRegistrarInsumos.Visible = true;

                btnRegistrarReceta.Enabled = true;
                btnRegistrarReceta.Visible = true;

                btnVerInsumos.Visible = true;

                btnVerInsumos.Enabled = true;
                btnVerInsumos.Visible = true;

                tabDatosPaciente.Enabled = true;
                tabDatosConsulta.Enabled = true;
                
            }

            if (ConsultationId != null)
            {

                idConsultation = long.Parse(ConsultationId);
                presenter = new PresenterShowConsultation(this);

                consultation = presenter.ShowConsultationAppointment();
                
                label2.Text = consultation.ConsultationId.ToString();
                label3.Text = AppointmentId;


                llenarFormulario();
            }
        }

        Consultation consultation = new Consultation();
        long idConsultation;
        PresenterShowConsultation presenter;

        long IShowConsultation.ConsultationId
        {
            get
            {
                return idConsultation;
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        String [] FamilyHistorySplit;
        String [] PastMedicalHistorySplit;
        String [] PersonalHistorySplit;
        String[] GeneralSymptomsSplit;

        public void llenarFormulario()
        {
            txtPatientId.Text = consultation.PatientId.ToString();
            txtDoctorId.Text = consultation.DoctorId.ToString();
            txtAppointmentId.Text = consultation.AppointmentId.ToString();


            FamilyHistory = consultation.FamilyHistory.ToString();
            txtFamilyHistoryDetails.Text = consultation.FamilyHistoryDetails.ToString();
            PastMedicalHistory = consultation.PastMedicalHistory.ToString();
            txtPastMedicalHistoryDetails.Text = consultation.PastMedicalHistoryDetails.ToString();
            PersonalHistory = consultation.PersonalHistory.ToString();
            txtPersonalHistoryDetails.Text = consultation.PersonalHistoryDetails.ToString();

            txtHeartRate.Text = consultation.HeartRate.ToString();
            txtBloodPressure.Text = consultation.BloodPressure.ToString();
            txtRespirationRate.Text = consultation.RespirationRate.ToString();
            txtTemperature.Text = consultation.Temperature.ToString();
            txtCurrentWeight.Text = consultation.CurrentWeight.ToString();
            txtHeight.Text = consultation.Height.ToString();
            txtIdealWeight.Text = consultation.IdealWeight.ToString();

            txtChiefComplaints.Text = consultation.ChiefComplaints.ToString();
            GeneralSymptoms = consultation.GeneralSymptoms.ToString();

            cbxCurrentCondition.Text = consultation.CurrentCondition.ToString();
            txtCurrentConditionDetails.Text = consultation.CurrentConditionDetails.ToString();
            txtPreviousDiagnoses.Text = consultation.PreviousDiagnoses.ToString();
            txtPreviouslyEmployedTherapeutic.Text = consultation.PreviouslyEmployedTherapeutic.ToString();

            txtDigestiveApparatus.Text = consultation.DigestiveApparatus.ToString();
            txtCardiovascularApparatus.Text = consultation.CardiovascularApparatus.ToString();
            txtRespiratoryApparatus.Text = consultation.RespiratoryApparatus.ToString();
            txtUrinaryApparatus.Text = consultation.UrinaryApparatus.ToString();
            txtGenitalApparatus.Text = consultation.GenitalApparatus.ToString();
            txtHematologicApparatus.Text = consultation.HematologicApparatus.ToString();
            txtEndocrineSystem.Text = consultation.EndocrineSystem.ToString();
            txtOsteomuscularSystem.Text = consultation.MusculoskeletalSystem.ToString();
            txtNervousSystem.Text = consultation.NervousSystem.ToString();
            txtSensorialSystem.Text = consultation.SensorialSystem.ToString();
            txtPsicosomatic.Text = consultation.Psychosomatic.ToString();

            txtGeneralExamination.Text = consultation.GeneralExamination.ToString();
            txtExaminationHead.Text = consultation.ExaminationHead.ToString();
            txtExaminationNeck.Text = consultation.ExaminationNeck.ToString();
            txtExaminationChest.Text = consultation.ExaminationChest.ToString();
            txtExaminationAbdomen.Text = consultation.ExaminationAbdomen.ToString();
            txtExaminationMembers.Text = consultation.ExaminationMembers.ToString();
            txtExaminationGenitals.Text = consultation.ExaminationGenitals.ToString();

            txtComments.Text = consultation.Comments.ToString();
            txtDiagnosis.Text = consultation.Diagnosis.ToString();
            txtPrognosis.Text = consultation.Prognosis.ToString();
            txtTreatment.Text = consultation.Treatment.ToString();

            FamilyHistorySplit = FamilyHistory.Split('-');
            PastMedicalHistorySplit = PastMedicalHistory.Split('-');
            PersonalHistorySplit = PersonalHistory.Split('-');
            GeneralSymptomsSplit = GeneralSymptoms.Split('-');

            revisarSplits();
        }

        public void revisarSplits()
        {
            foreach (string s in FamilyHistorySplit)
            {
                if (s == "1")
                    chkTB.Checked = true;
                if (s == "2")
                    chkDiabetes.Checked = true;
                if (s == "3")
                    chkMellitus.Checked = true;
                if (s == "4")
                    chkHypertension.Checked = true;
                if (s == "5")
                    chkCarcinomas.Checked = true;
                if (s == "6")
                    chkHeart.Checked = true;
                if (s == "7")
                    chkLiver.Checked = true;
                if (s == "8")
                    chkKidney.Checked = true;
                if (s == "9")
                    chkEndocrine.Checked = true;
                if (s == "10")
                    chkMentalIllness.Checked = true;
                if (s == "11")
                    chkEpilepsy.Checked = true;
                if (s == "12")
                    chkAsthma.Checked = true;
                if (s == "13")
                    chkHematologicDiseases.Checked = true;
            }

            for (int i = 0; i < PersonalHistorySplit.Length; i++ )
            {
                if (PersonalHistorySplit[i] == "1")
                {
                    chkSmoking.Checked = true;
                    txtSmoking.Text = PersonalHistorySplit[i + 1].ToString();
                    i++;
                }
                if (PersonalHistorySplit[i] == "2")
                {
                    chkAlcoholism.Checked = true;
                    txtAlcoholism.Text = PersonalHistorySplit[i + 1].ToString();
                    i++;
                }
                if (PersonalHistorySplit[i] == "3")
                {
                    chkAddiction.Checked = true;
                    txtAddiction.Text = PersonalHistorySplit[i + 1].ToString();
                    i++;
                }
                if (PersonalHistorySplit[i] == "4")
                {
                    chkPhysicalActivity.Checked = true;
                    txtPhysicalActivity.Text = PersonalHistorySplit[i + 1].ToString();
                    i++;
                }
                if (PersonalHistorySplit[i] == "5")
                    chkImmunizations.Checked = true;
                if (PersonalHistorySplit[i] == "6")
                    chkAllergies.Checked = true;
            }

            foreach (string s in PastMedicalHistorySplit)
            {
                if (s == "1")
                    chkChildhoodInfectious.Checked = true;
                if (s == "2")
                    chkTBPato.Checked = true;
                if (s == "3")
                    chkVenerealDiseases.Checked = true;
                if (s == "4")
                    chkTyphoidFever.Checked = true;
                if (s == "5")
                    chkSalmonellosis.Checked = true;
                if (s == "6")
                    chkPneumonia.Checked = true;
                if (s == "7")
                    chkMalaria.Checked = true;
                if (s == "8")
                    chkParasites.Checked = true;
                if (s == "9")
                    chkAllergicDiseases.Checked = true;
                if (s == "10")
                    chkArticularConditions.Checked = true;
                if (s == "11")
                    chkSurgeries.Checked = true;
                if (s == "12")
                    chkHospitalizations.Checked = true;
                if (s == "13")
                    chkInjuries.Checked = true;
                if (s == "14")
                    chkLossConsciousness.Checked = true;
                if (s == "15")
                    chkDrugIntolerance.Checked = true;
                if (s == "16")
                    chkTransfusions.Checked = true;
            }

            




            
        }

        private void btnVerInsumos_Click(object sender, EventArgs e)
        {
            GetAllMedicalSupplies getall = new GetAllMedicalSupplies();
            this.Visible = false;
            if (getall.ShowDialog() == DialogResult.Cancel)
                this.Visible = true;
        }

        private void btnVerInsumos_Click_1(object sender, EventArgs e)
        {
            GetAllMedicalSupplies getall = new GetAllMedicalSupplies();
            this.Visible = false;
            if (getall.ShowDialog() == DialogResult.Cancel)
                this.Visible = true;
        }


        
    }
}
